<?php

class configModel extends Model {
    
    protected $_primaryKey = 'id';
    
    const CONFIG_SET = 'CONFIG_SET_';
    
    public function getAllType() {
        return [
            '1' => '文本输入',
            '2' => '图片上传'
        ];
    }
    
    public function updateById($id, $data) {
        if ($this->update($data, $this->_primaryKey . '=?', $id)) {
            Cache_Memcache::getInstance()->delete(self::CONFIG_SET.$data['ename']);
            Cache_Memcache::getInstance()->delete($this->getTableName() . $id);
            return true;
        }
        return false;
    }

    public function findById($id) {
        if (!$info = Cache_Memcache::getInstance()->get($this->getTableName() . $id)) {
            if (!$info = $this->find($id)) {
                return false;
            }
            Cache_Memcache::getInstance()->set($this->getTableName() . $id, $info);
        }
        return $info;
    }
    
    public function findByEname($ename) {
        $key = self::CONFIG_SET.$ename;
        $info = Cache_Memcache::getInstance()->get($key);
        if($info!==false) {
            return $info;
        }
        if($info = $this->getOne('ename=?', $ename)) {
            Cache_Memcache::getInstance()->set($key, $info['kvalue']);
            return $info['kvalue'];
        }
        return '';
    }
}